package com.jpress.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

@Entity
@Table(name="noticia")
@NamedQueries({
	@NamedQuery(name="Noticia.countNoticias", query="SELECT COUNT (n) FROM Noticia n"),
	@NamedQuery(name="Noticia.findAllByUsuarioCodigo", query="SELECT n FROM Noticia n WHERE n.usuario.codigo = :usuarioCodigo"),
	@NamedQuery(name="Noticia.findAll", query="SELECT n FROM Noticia n"),
	@NamedQuery(name="Noticia.searchByTituloAndConteudo", query="SELECT n FROM Noticia n WHERE n.titulo LIKE :titulo AND n.conteudo LIKE :conteudo")
})
public class Noticia implements Serializable{

	private static final long serialVersionUID = -871912432589992799L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="codigo")
	private int codigo;
	
	@ManyToOne
	private Usuario usuario; //autor da not�cia
	
	@Column(name="titulo", nullable=false, length=200)
	private String titulo;
	
	@Column(name="chamada", nullable=false, length=1000)
	private String chamada;		
	
	@Column(name="conteudo", nullable=false, length=15000)
	private String conteudo;
	
	@Column(name="ip", nullable=false, length=50)
	private String ip;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="data", nullable=false)
	private Date dataCadastro;
	
	@Column(name="ativo")
	private boolean ativo;
	
	@ManyToMany
	@JoinTable(name="noticia_tag",
		joinColumns=@JoinColumn(name="noticia_codigo"),
		inverseJoinColumns=@JoinColumn(name="tag_codigo"))
	private List<Tag> tags;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="noticia")
	private List<Ranking> rankings;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="noticia")
	private List<Midia> midias;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="noticia")
	private List<Comentario> comentarios;
	
	public Noticia(){
		tags = new ArrayList<Tag>();
		rankings = new ArrayList<Ranking>();
		midias = new ArrayList<Midia>();
		comentarios = new ArrayList<Comentario>();
	}

	public int getCodigo() {
		return codigo;
	}

	public void setCodigo(int codigo) {
		this.codigo = codigo;
	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public String getTitulo() {
		return titulo;
	}

	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}

	public String getChamada() {
		return chamada;
	}

	public void setChamada(String chamada) {
		this.chamada = chamada;
	}

	public String getConteudo() {
		return conteudo;
	}

	public void setConteudo(String conteudo) {
		this.conteudo = conteudo;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public Date getDataCadastro() {
		return dataCadastro;
	}

	public void setDataCadastro(Date dataCadastro) {
		this.dataCadastro = dataCadastro;
	}

	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	public List<Ranking> getRankings() {
		return rankings;
	}

	public void setRankings(List<Ranking> rankings) {
		this.rankings = rankings;
	}

	public List<Midia> getMidias() {
		return midias;
	}

	public void setMidias(List<Midia> midias) {
		this.midias = midias;
	}

	public List<Comentario> getComentarios() {
		return comentarios;
	}

	public void setComentarios(List<Comentario> comentarios) {
		this.comentarios = comentarios;
	}
	
	@Override
	public boolean equals(Object object){
		return EqualsBuilder.reflectionEquals(this, object);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}		
}
